package wwk.common.util;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.net.Uri;
import android.telephony.TelephonyManager;

public class SysUtil
{
	public static String getPackageName(Context context)
	{
		 ApplicationInfo ppplicationInfo=context.getApplicationInfo();
		 return ppplicationInfo.packageName;
	}
	
	public static int getVersionCode(Context context) 
	{
		String packageName=SysUtil.getPackageName(context);
        int verCode = -1;
        try 
        {
            verCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
        } 
        catch (NameNotFoundException e) 
        {

        }
        return verCode;
    }
   
    public static String getVersionName(Context context) 
    {
    		String packageName=SysUtil.getPackageName(context);
        String verName = "";
        try 
        {
            verName = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
        } 
        catch (NameNotFoundException e) 
        {
        }
        return verName;   
    }
	
	/*
	 * ��activity.setContentView()֮ǰ��
	 *   requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);֮����á�
	 */
	public static void setFullScreen(Activity activity)
	{
		activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); //����ȫ��
	}
	
	/*
	 * ������requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);ͬʱʹ��*/
	public static void setNoTitle(Activity activity)
	{
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}
	
	public static int getStatusBarHeight(Activity activity)
	{
		//Ϊʲôֻ�����߳��е��ã�����Ч��
		
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top; 
//		int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//		//statusBarHeight�����������״̬���ĸ߶�
//		int titleBarHeight = contentTop - statusBarHeight;
		
		return statusBarHeight;
	}
	
	/**
	 * ����
	 * @param activity
	 */
	public static void setOrientationPortrait(Activity activity)
	{
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_NOSENSOR
	}
	
	/**
	 * ���� ActivityInfo.SCREEN_ORIENTATION_NOSENSOR | 
	 * @param activity
	 */
	public static void setOrientationLandscape(Activity activity)
	{
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_NOSENSOR
	}
	
	public static void setSoftInputStateHidden(Activity activity)
	{
		activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
				|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
				|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);  
	}
	
	public static void setCustomTitle(Activity activity)
	{
		activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	}
	
	public static DisplayMetrics getDisplayMetrics(Activity activity)
	{
	   DisplayMetrics dm=new DisplayMetrics();
	   activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
	   return dm;
	}
	
	public static void printRuntimeMemory(String tag, String msg)
	{
		Runtime.getRuntime().runFinalization();
		
		long maxMemory=Runtime.getRuntime().maxMemory();
		long totalMemory=Runtime.getRuntime().totalMemory();
		long freeMemory=Runtime.getRuntime().freeMemory();
		
		Log.d(tag,msg);
		
		Log.d(tag+" maxMemory",""+maxMemory);
		Log.d(tag+" totalMemory",""+totalMemory);
		Log.d(tag+" freeMemory",""+freeMemory);
	}
	
	/**
	 * ��TelephonyManager�л�ȡimei,����Ϣ
	 * @param context
	 */
	public static TelephonyManager getTelephonyManager(Context context)
	{
		TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager;
	}
	
	public static boolean isServiceExists(Context context, String className) 
	{
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);

        int size=serviceList.size();
        if(size<=0) 
        {
            return false;
        }

        for(int i = 0; i < size; i++) 
        {
            RunningServiceInfo serviceInfo = serviceList.get(i);
            ComponentName serviceName = serviceInfo.service;

           if(serviceName.getClassName().equals(className))
           {
                return true;
           }
        } 
        
       return false;
    }
	
	//打开APK
	public static void openFile(String filePathName,Context context) 
	{
		if(filePathName==null || filePathName.length()==0)
			return;
		
		if(!FileUtil.isFileExists(filePathName))
		{
			return;
		}
		
	    Intent intent = new Intent();
	    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    intent.setAction(android.content.Intent.ACTION_VIEW);
	    intent.setDataAndType(Uri.fromFile(new File(filePathName)),
	                    "application/vnd.android.package-archive");
	    context.startActivity(intent);
    }
}